var util = require('../../utils/request.js');
var globalData = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    date: '',
  },

  getPersonInfo: function() {
    var that = this
    util.req('/member/detail', {
      memberId: globalData.memberId
    }, function(success) {
      console.log(success.resultContent)
      var info = {
        headImg: success.resultContent.headImg,
        name: success.resultContent.nickName,
        isIdentity: (success.resultContent.isIdentity == false) ? '未实名' : '已实名',
        phone: success.resultContent.phone,
        sex: success.resultContent.sex || '',
        birthday: success.resultContent.birthday,
        email: success.resultContent.email
      }
      that.setData({
        info: info,
        date: new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-' + new Date().getDate()
      })
      console.log(that.data.info.sex);
    })
  },
  //添加车牌
  addCard: function() {
    wx.navigateTo({
      url: '../addCard/addCard',
    })
  },
  realName: function() {
    if (this.data.info.isIdentity == '未实名') {
      wx.navigateTo({
        url: '../realName/realName',
      })
    } else {
      wx.showToast({
        title: '你已经实名认证啦',
        icon: 'none'
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getPersonInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  _setSex() {
    var that = this;
    var info = this.data.info;

    var itemList = ['男', '女'];
    wx.showActionSheet({
      itemList,
      success(res) {
        info.sex = itemList[res.tapIndex];
        that.setData({
          info
        })
        that.edit(info);
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },
  bindDateChange: function(e) {
    var info = this.data.info;
    info.birthday = e.detail.value;
    this.edit(info);
  },
  edit(info) {
    var that = this;
    var url = '/member/change';
    info.sex = info.sex == '男' ? 1 : 0;
    var userInfo = {
      sex: info.sex,
      birthday: info.birthday,
      email: info.email
    }

    var paramsData = {
      memberInfo: JSON.stringify(userInfo),
      memberId: globalData.memberId
    };
    util.req(url, paramsData, function(res) {
      var res = res.resultContent;
      var info = {
        headImg: res.headImg,
        name: res.nickName,
        isIdentity: (res.isIdentity == false) ? '未实名' : '已实名',
        phone: res.phone,
        sex: res.sex || '',
        birthday: res.birthday,
        email: res.email
      }

      console.log(info);
      that.setData({
        info: info
      })
      console.log(that.data.info.sex);
    })
  },
  _jump(evt){
    util.jump(evt);
  }
})